package com.sunyard.manage.util;


import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


/**
 * HTTP工具类
 *
 * @author lixiangyang
 */
public class BeanUtils {

    /**
     * Description
     */
    private static Log log = LogFactory.getLog(BeanUtils.class);

    /**
     * 将javabean实体类转为map类型，然后返回一个map类型的值
     */
    public static Map<String, String> beanToMap(Object obj) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        Map<String, String> newMap = new HashMap<String, String>();
        try {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                }
            }
            for (Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() instanceof String) {
                    newMap.put(entry.getKey(), (String) entry.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newMap;
    }


    /**
     * Description
     */
//    public static String MapToUrlString(Map<String, String> map) {
//        StringBuffer sb = new StringBuffer();
//        for (Entry<String, String> vo : map.entrySet()) {
//            sb.append(vo.getKey() + "=" + vo.getValue() + "&");
//        }
//        String s = sb.toString();
//        if (StringUtils.isNotBlank(s)) {
//            return s.substring(0, s.length() - 1);
//        } else {
//            return s;
//        }
//    }
}
